package cn.edu.scau.cmi.front.service;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.engine.internal.Collections;
import org.hibernate.hql.internal.ast.QueryTranslatorImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Feature;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.domain.Standard;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.front.dao.CmiEventDAO;
import cn.edu.scau.cmi.front.dao.CmiKnowledgeBaseDAO;

/**
 * Spring service that handles CRUD requests for FrontService
 * 
 */

@Service("FrontService")

@Transactional
public class FrontServiceImpl extends CmiEmaService implements FrontService {

	@Autowired
	CmiFoodtypeService cmiFoodtypeService;

	@Autowired
	CmiKnowledgeBaseDAO cmiKnowledgeBaseDAO;

	@Autowired
	CmiEventDAO cmiEventDAO;

	public FrontServiceImpl() {
	}

	public Set<String> getKnowledgeBaseBasicAttributes() {

		Set<String> basicAttributes = new LinkedHashSet<>();
		basicAttributes.add("StandardName");
		basicAttributes.add("StandardLevel");
		basicAttributes.add("StandardType");
		basicAttributes.add("FeatureName");
		return basicAttributes;
	}

	public Set<Object> getRecordsForKnowledgeBase(CmiPagination page) {

		// 判断是否经过跳转页框的输入
		if (page.getInputPagination()!=0) {
			page.setCurrentPagination(page.getInputPagination());
		}
		Set<Object> objects = null;
		// 设置搜索的初始页码
		page.setSearchBeginCursor((page.getCurrentPagination() - 1) * page.getPaginationSize());
			

//		// 表记录的查询
//		String sql = CmiSqlUtil.createSelectSql4Standard(page);
//
//		objects = cmiKnowledgeBaseDAO.findKnowledgeItemsByCompositeProperities(sql, page.getSearchBeginCursor(),
//				page.getPaginationSize());
		// 分页类的初始化
//		page.update(countKnowledgeItems(page));
		System.out.println("countKnowledgeItems:-->>" + countKnowledgeItems(page));
		return objects;
	}

	public Set<Object> getRecordsForKnowledgeBaseByFeature(CmiPagination page, String sqlString) {

		// 判断是否经过跳转页框的输入
		if (page.getInputPagination()!=0) {
			page.setCurrentPagination(page.getInputPagination());
		}
		Set<Object> objects = null;
		// 设置搜索的初始页码
		page.setSearchBeginCursor((page.getCurrentPagination() - 1) * page.getPaginationSize());

		// 表记录的查询
//		String sql = CmiSqlUtil.createSelectSql4Standard(page);

//		if (sql.contains("where")) {
//			sql += " and " + sqlString;
//		} else {
//			sql += " where " + sqlString;
//		}

//		objects = cmiKnowledgeBaseDAO.findKnowledgeItemsByCompositeProperities(sql, page.getSearchBeginCursor(), page.getPaginationSize());
		// 分页类的初始化
//		page.update(countKnowledgeItemsByFeature(page, sqlString));
		System.out.println("countKnowledgeItems:-->>" + countKnowledgeItemsByFeature(page, sqlString));
		return objects;
	}

	@Transactional
	public Integer countKnowledgeItems(CmiPagination page) {
//		String sql = CmiSqlUtil.createCountSql4Featurestandard(page);
		return null;
	}

	@Transactional
	public Integer countKnowledgeItemsByFeature(CmiPagination page, String sqlString) {

//		String sql = CmiSqlUtil.createCountSql4Featurestandard(page);
//		if (sql.contains("where")) {
//			sql += " and " + sqlString;
//		} else {
//			sql += " where " + sqlString;
//		}
		return null;
	}
	/*
	 * public Set<Picture> getNewestPictures(int needExhibinationPicutreQuantity) {
	 * HashSet<Picture> newestPictureSet = new HashSet<>(); Set<Picture> pictures =
	 * cmiPictureDAO.findPicturesByIdDesc(-1, 200); Set<Event> events = new
	 * HashSet<>(); for (Picture picture : pictures) { if(events.size() >=
	 * needExhibinationPicutreQuantity) break; Event event = picture.getEvent();
	 * if(!events.contains(event)) { newestPictureSet.add(picture);
	 * events.add(event); } } return newestPictureSet; }
	 */

//	获取不同食品类型的在首页界面上显示最新的图片，目前默认是5张。
	// 此方法返回的pictures还可以优化，因为此处返回的pictures在frontIndex页面只使用到了picture类的event和path属性，其他属性并没有
	// 用到，因此，可以将findPicturesByIdDesc的select * from picture优化成select
	// picture.path,picture.event from picture
	// 当然，接受参数要使用Object[]数组---->>唐锦文
	public Set<Picture> getExhibinatePictures(int needExhibinationPicutreQuantity, int foodtypeId) {
		// 根据食品根id找到所有子孙 descendant （后代）食品id，并放入集合
		Set<Integer> foodtypeIds = cmiFoodtypeService.getDescendantIds(foodtypeId);
//		页面展示的图片集合
		HashSet<Picture> exhibinatePictureSet = new HashSet<>();
		// 找出最新的200条事件
//		Set<Event> events = cmiEventDAO.findEventsByIdDesc(-1, 4000);

//		从最新的图片获取5个。Id最大的，然后依次筛选，每5个获取
		Set<Picture> picutres = new HashSet<Picture>();
//		用于统计准备好的图片的数量，
//		Set<Event> events=new HashSet<Event>();

		int maxRows = 100;

		for (int startResult = 0; exhibinatePictureSet.size() < 5; startResult = startResult + maxRows) {
			picutres = cmiPictureDAO.findPicturesByIdDesc(startResult, maxRows);// 从指定的编号开始获取最新的15个图片，直到有效的5个为止。
			Iterator<Picture> picturesIterator = picutres.iterator();
			while (picturesIterator.hasNext() && exhibinatePictureSet.size() < needExhibinationPicutreQuantity) {
				Picture picture = picturesIterator.next();
				if (foodtypeIds.contains(picture.getEvent().getFoodbatch().getFood().getFoodtype().getId())) // 图片对应的事件的食物是自定的食物类型，这个事件就是有效事件
					// events.add(picture.getEvent());
					exhibinatePictureSet.add(picture);
			}
		}
		return exhibinatePictureSet;
	}

	// 上面方法的重载，把传入参数由 根id 换成 根据食品根id找到所有子孙 descendant（后代）食品id的集合 ==> 杨宏宇 2020-1-6
	public Set<Picture> getExhibinatePictures(int needExhibinationPicutreQuantity, Set<Integer> foodtypeIds) {
//		TODO 好像不需要了
		if (foodtypeIds == null)
			foodtypeIds = cmiFoodtypeService.getDescendantIds(561);
//		页面展示的图片集合
		HashSet<Picture> exhibinatePictureSet = new HashSet<>();
		// 找出最新的200条事件
//		Set<Event> events = cmiEventDAO.findEventsByIdDesc(-1, 4000);

//		从最新的图片获取5个。Id最大的，然后依次筛选，每5个获取
		Set<Picture> picutres = new HashSet<Picture>();
//		用于统计准备好的图片的数量，
//		Set<Event> events=new HashSet<Event>();

		int maxRows = needExhibinationPicutreQuantity * 4;
		Set<Boolean> eventStatusSet = new HashSet<>();
//		eventStatusSet.add(false);
		eventStatusSet.add(true);
		Set<Picture> picturesWithPictureByFoodtypeIdsDesc = cmiEventDAO.findPicturesWithPictureByEventStatusAndFoodtypeIdsDesc(eventStatusSet, foodtypeIds, 0, maxRows);
		
		for (Picture picture : picturesWithPictureByFoodtypeIdsDesc) {
			if (exhibinatePictureSet.size() >= needExhibinationPicutreQuantity) break;
			boolean anyMatch = picutres.parallelStream().anyMatch(new Predicate<Picture>() {
				@Override
				public boolean test(Picture t) {
					return t.getEvent().getId().equals(picture.getEvent().getId());
				}
			});
			if (!anyMatch) exhibinatePictureSet.add(picture);
		}
		
//		Set<Event> events = cmiEventDAO.findEventsWithPictureByFoodtypeIdsDesc(foodtypeIds, 0, maxRows);
//		for (Event event : events) {
//			if (exhibinatePictureSet.size() >= needExhibinationPicutreQuantity)
//				break;
//			if (event.getPicturesForEvent().size() >= 1)
//				exhibinatePictureSet.add((Picture) event.getPicturesForEvent().toArray()[0]);
//		}

//		for(int startResult=0;exhibinatePictureSet.size()<needExhibinationPicutreQuantity;startResult=startResult+maxRows) {
//			picutres=cmiPictureDAO.findPicturesByIdDesc(startResult,maxRows);//从指定的编号开始获取最新的15个图片，直到有效的5个为止。
//			Iterator<Picture> picturesIterator = picutres.iterator();
//			while(picturesIterator.hasNext() && exhibinatePictureSet.size()<needExhibinationPicutreQuantity){
//				Picture picture = picturesIterator.next();
//				if(foodtypeIds.contains(picture.getEvent().getFoodbatch().getFood().getFoodtype().getId()))  //图片对应的事件的食物是自定的食物类型，这个事件就是有效事件
//				//events.add(picture.getEvent());
//				exhibinatePictureSet.add(picture);
//			}
//		}
		return exhibinatePictureSet;
	}

//	// 用递归，有风险 改成下面的
//	public Set<Integer> findAllFoodtypeIdsByParent(int rootFoodtypeId,Set<Integer> foodtypeIds){
//		Set<Integer> foodtypeIdsByParent = foodtypeDao.findFoodtypeIdsByParent(rootFoodtypeId);  // 就算找不到数据，返回的也是一个Set对象，下一行的判断没意义
//		if(foodtypeIdsByParent!=null) {
//			foodtypeIds.addAll(foodtypeIdsByParent);
//			for(Integer id:foodtypeIdsByParent) {
//				findAllFoodtypeIdsByParent(id,foodtypeIds);
//			}
//		}
//		return foodtypeIds;
//	}

//	public Set<Integer> findAllFoodtypeIdsByParent(int rootFoodtypeId,Set<Integer> foodtypeIds){
//		
//		ArrayList<Integer> foodtypeIdList = new ArrayList<>();
//		foodtypeIdList.add(rootFoodtypeId);
//		for(int i = 0; i < foodtypeIdList.size(); i++) {
//			Set<Integer> foodtypeIdsByParent = foodtypeDao.findFoodtypeIdsByParent(foodtypeIdList.get(i));  
//			if(i == 0) foodtypeIdList.clear();
//			if(!foodtypeIdsByParent.isEmpty()) {
//				foodtypeIdList.addAll(foodtypeIdsByParent);
//			}
//		}
//		foodtypeIds.addAll(foodtypeIdList);
//		
//		return foodtypeIds;
//	}

	@Override
	public Set<Picture> getNewestEventsReprestativePictures(int needExhibinationPicutreQuantity) {
//		利用时间或者id逆序迭代event,找到record, 找到recordDetail, 再第一个图片，如果存在，就放到图片的集合中；
//		TODO Be Tested
//		Set<Event> eventSet = eventDAO.findEventsByIdDesc(-1, 200);
		// Set<Event> eventSet = cmiEventDAO.findEventsByIdDesc(-1, 4000);
		Set<Event> eventSet = eventDAO.findAllEvents();
		Iterator<Event> eventSetIterator = eventSet.iterator();

		Set<Picture> newestPictureSet = new HashSet<Picture>();
		while (eventSetIterator.hasNext() && newestPictureSet.size() < needExhibinationPicutreQuantity) {
			Event event = eventSetIterator.next();
//			每个事件的记录，然后迭代获取这些记录的明细
			Set<Event> events = event.getEventsForParent();// 事件的子事件
			if (events != null) {
				Iterator<Event> eventsIterator = events.iterator();
				while (eventsIterator.hasNext()) {
					Event newEvent = eventsIterator.next();
					Set<Event> recordDetails = newEvent.getEventsForParent();// 事件的子事件的子事件
					if (recordDetails != null) {
						Iterator<Event> recordsIterator = recordDetails.iterator();
						while (recordsIterator.hasNext()) {
							Event recordDetail = recordsIterator.next();
							Set<Picture> pictures = recordDetail.getPicturesForEvent();
							Picture efficePicture = null;
							if (pictures != null) {
								// 记录明细只需要一张典型的在前端展示的图片，目前使用id号最小的图片。
								// getPicture(pictures);
								//
								// TODO 从图片集合中获取第一张图片，后期的修改陈获取最有典型意义的图片
								Iterator<Picture> picutureIterator = pictures.iterator();
								while (picutureIterator.hasNext()) {
									Picture picture = picutureIterator.next();
									if (efficePicture == null
											|| efficePicture.getId().intValue() < picture.getId().intValue()) {
										efficePicture = picture;
									}
								}
								newestPictureSet.add(efficePicture);
							}
						}
					}
				}
			}
		}
		return newestPictureSet;
	}

	@Override
	public Map<Event, Picture> getRepresentativePictureOfEvents(LinkedHashSet<Event> events) {

		Map<Event, Picture> eventPictureMap = new LinkedHashMap<Event, Picture>();
		Iterator<Event> eventIterator = events.iterator();
		while (eventIterator.hasNext()) {
			Event event = eventIterator.next();
//			一个事件可以有多个图片，获取
			Picture picture = getRepresentativePictureOfEvent(event);
			eventPictureMap.put(event, picture);
		}
		return eventPictureMap;
	}
	
	
	@Override
	public Picture getRepresentativePictureOfEvent(Event event) {
		String regex = "snapshot_"; // 现在的网页快照可以用这个正则表达式匹配;当前更改为网页快照优先级最低 —— 杨宏宇
		Picture representativePicture = null;
		// 得到事件的所有子事件 <======= 这里应该是得到事件的关联图片吧 —— 杨宏宇
		Set<Picture> eventPictures = event.getPicturesForEvent();
		for (Picture picture : eventPictures) {
			if (picture.getPath().startsWith(regex)) continue;	// 不考虑快照
			if (representativePicture == null || representativePicture.getId() < picture.getId()) // 取id大的
				representativePicture = picture;
		}
//		Iterator<Picture> eventPicturesIterator = eventPictures.iterator();
//		if (eventPicturesIterator.hasNext()) {
//			Picture lastestPicture = eventPicturesIterator.next();
//			for (Picture pt : eventPictures) {
//				// 更改开始 —— 杨宏宇
//				if (lastestPicture.getPath().matches(regex)) { // 匹配成功，lastestPicture为网页快照
//					if (pt.getPath().matches(regex)) { // 同为网页快照，取id大的
//						lastestPicture = lastestPicture.getId() > pt.getId() ? lastestPicture : pt;
//					} else { // 网页快照优先级最低
//						lastestPicture = pt;
//					}
//				} else { // 匹配失败，lastestPicture不为网页快照
//					if (!pt.getPath().matches(regex)) { // 都不为网页快照，取id大的
//						lastestPicture = lastestPicture.getId() > pt.getId() ? lastestPicture : pt;
//					}
//				}
//				// 更改结束 —— 杨宏宇
////					lastestPicture=lastestPicture.getId()>pt.getId()?lastestPicture:pt; 原代码
//			}
//			representativePicture = lastestPicture;
//		}
		return representativePicture;
	}


	
	//	保存图片文件
	public boolean saveUploadFiles(List<MultipartFile> multipartFiles, String uploadDirectory, String fileName) {
		/**
		 * 保存图片/视频文件
		 */
		if (multipartFiles != null && multipartFiles.size() > 0) {
//			一个文件的时候，多个文件的时候分别处理
			for (int i = 0; i < multipartFiles.size(); i++) {
				MultipartFile multipartFile = multipartFiles.get(i);
				if (!multipartFile.isEmpty()) {
//					如果只有一个文件，不用添加序号，如果有多个文件，每个文件需要添加序号“_i”.另外，由于文件名没有后缀，需要添加后缀的名称 。
//					上传文件名的后缀
					String updateFileName=multipartFile.getOriginalFilename();
					String fileSuffix=updateFileName.substring(updateFileName.lastIndexOf("."));
					String newFileName = null;
					try {
						newFileName=uploadDirectory+File.separator+fileName+"_"+i+fileSuffix;
						File file = new File(newFileName);
						multipartFile.transferTo(file);
						System.out.println("文件已经保存了：" + file.getAbsolutePath());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return true;
//		return multipartFiles.size();
	}

//	@Override
//	public Picture getRepresentativePictureOfEvent(Event event) {
//
////		每个事件的记录，然后迭代获取这些记录的明细
//		Picture representativePicture=null;
//		//得到事件的所有子事件
//		Set<Event> eventEventdetails = event.getEventsForParent();
//		Iterator<Event> eventsIterator = eventEventdetails.iterator();
//		while (eventsIterator.hasNext()) {
//			Event newEvent = eventsIterator.next();
//			Set<Event> recordDetails = newEvent.getEventsForParent();
//			Iterator<Event> recordsIterator = recordDetails.iterator();
//			while(recordsIterator.hasNext()) {
//				Event records = recordsIterator.next();
//				Set<Picture> pictures = records.getPicturesForEvent();
//				
////				记录明细只需要一张典型的在前端展示的图片，目前使用id号最小的图片。
////				getPicture(pictures);
////
////				TODO 从图片集合中获取第一张图片，后期的修改陈获取最有典型意义的图片
//				Iterator<Picture> picutureIterator = pictures.iterator();
//				while(picutureIterator.hasNext()) {
//					Picture picture = picutureIterator.next();
//					if(representativePicture==null || representativePicture.getId().intValue()>picture.getId().intValue()) {
//						representativePicture=picture;
//					}
//				}
//			}
//		}
//		return representativePicture;
//	
//	}





	/*
	 * @Override void getFrontIndexObjects(List<Calendar> dates, Set<Picture>
	 * pictures, Set<Event> unsortedEventSet) { // TODO
	 * 使用抽取出来的getFrontEventDescriptions方法！！！ // 集合到数组，排序，Object[][]结构：事件id，事件最早报道时间
	 * 
	 * int recordQuanitityOfEvent = 0; for (Event e : unsortedEventSet) { if
	 * (e.getRecordsForEvent().size() != 0) { recordQuanitityOfEvent++; } }
	 * Object[][] earliestRecordOfEachEvent = new Object[recordQuanitityOfEvent][2];
	 * // 获取每个事件最早的报道时间，把最早的报道时间作为时间的默认事件！！！ int current = 0; for (Event event :
	 * unsortedEventSet) { if (event.getRecordsForEvent().size() == 0) { continue; }
	 * Set<Eventdetail> recordSet = event.getRecordsForEvent(); Eventdetail
	 * earliestRecord = recordSet.iterator().next(); for (Eventdetail eventdetail :
	 * recordSet) { if (eventdetail.getDate().compareTo(earliestRecord.getDate()) >
	 * 0) { earliestRecord = eventdetail; } } earliestRecordOfEachEvent[current][0]
	 * = event.getId(); earliestRecordOfEachEvent[current++][1] =
	 * earliestRecord.getDate(); } System.out.println(); // 根据最早的报道时间对每个事件排序 for
	 * (int i = 0; i < earliestRecordOfEachEvent.length - 1; i++) { for (int j = 0;
	 * j < earliestRecordOfEachEvent.length - 1 - i; j++) { if (((Calendar)
	 * earliestRecordOfEachEvent[j][1]).compareTo((Calendar)
	 * earliestRecordOfEachEvent[j + 1][1]) < 0) { Object[] temp = new Object[2];
	 * temp = earliestRecordOfEachEvent[j]; earliestRecordOfEachEvent[j] =
	 * earliestRecordOfEachEvent[j + 1]; earliestRecordOfEachEvent[j + 1] = temp; }
	 * } }
	 * 
	 * 
	 * // 把排序好的Object[][]分别放到事件、图片、时间集合中，只获取4个最新的事件以及关联的图片-梁早清添加的注释 int tempCount =
	 * recordQuanitityOfEvent >= 4 ? 4 : recordQuanitityOfEvent; for (int i = 0; i <
	 * tempCount; i++) { dates.add((Calendar) earliestRecordOfEachEvent[i][1]);
	 * Set<Eventdetail> records = eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getRecordsForEvent(); int found = 0; for
	 * (Eventdetail eventdetail : records) { if (found == 1) { break; } else if
	 * (eventdetail.getRecordetailsForRecord().size() != 0) { Set<Eventdetailreport>
	 * recordetails = eventdetail.getRecordetailsForRecord(); for (Eventdetailreport
	 * eventdetailreport : recordetails) { if
	 * (eventdetailreport.getPicturesForRecordetail().size() != 0) { Set<Picture>
	 * picture = eventdetailreport.getPicturesForRecordetail();
	 * pictures.add(picture.iterator().next()); found = 1; break; } } } } } }
	 */

	/*
	 * private Set<String> getFrontEventDescriptions(List<Calendar> dates,
	 * Set<Picture> pictures, int timeQuantum, int evenType, String sort, Set<Event>
	 * unsortedEventSet) { int count = 0; for (Event e : unsortedEventSet) { if
	 * (e.getRecordsForEvent().size() != 0) { count++; } } Object[][]
	 * earliestRecordOfEachEvent = new Object[count][2]; int current = 0; for (Event
	 * e : unsortedEventSet) { if (e.getRecordsForEvent().size() == 0) { continue; }
	 * Set<Eventdetail> eventdetail = e.getRecordsForEvent(); Eventdetail
	 * earliestRecord = eventdetail.iterator().next(); for (Eventdetail r :
	 * eventdetail) { if (r.getDate().compareTo(earliestRecord.getDate()) > 0) {
	 * earliestRecord = r; } } earliestRecordOfEachEvent[current][0] = e.getId();
	 * earliestRecordOfEachEvent[current++][1] = earliestRecord.getDate(); }
	 * 
	 * for (int i = 0; i < earliestRecordOfEachEvent.length - 1; i++) { for (int j =
	 * 0; j < earliestRecordOfEachEvent.length - 1 - i; j++) { if
	 * ("timeDes".equals(sort)) { if (((Calendar)
	 * earliestRecordOfEachEvent[j][1]).compareTo((Calendar)
	 * earliestRecordOfEachEvent[j + 1][1]) < 0) { Object[] temp = new Object[2];
	 * temp = earliestRecordOfEachEvent[j]; earliestRecordOfEachEvent[j] =
	 * earliestRecordOfEachEvent[j + 1]; earliestRecordOfEachEvent[j + 1] = temp; }
	 * } else if ("timeAsc".equals(sort)) { if (((Calendar)
	 * earliestRecordOfEachEvent[j][1]).compareTo((Calendar)
	 * earliestRecordOfEachEvent[j + 1][1]) > 0) { Object[] temp = new Object[2];
	 * temp = earliestRecordOfEachEvent[j]; earliestRecordOfEachEvent[j] =
	 * earliestRecordOfEachEvent[j + 1]; earliestRecordOfEachEvent[j + 1] = temp; }
	 * } } }
	 * 
	 * 
	 * for (int i = 0; i < earliestRecordOfEachEvent.length; i++) { if (timeQuantum
	 * == 0) { if (evenType == 0) { dates.add((Calendar)
	 * earliestRecordOfEachEvent[i][1]); Set<Eventdetail> records =
	 * eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getRecordsForEvent(); int found = 0; for
	 * (Eventdetail r : records) { if (found == 1) { break; } else if
	 * (r.getRecordetailsForRecord().size() != 0) { Set<Eventdetailreport>
	 * recordetails = r.getRecordetailsForRecord(); for (Eventdetailreport rd :
	 * recordetails) { if (rd.getPicturesForRecordetail().size() != 0) {
	 * Set<Picture> picture = rd.getPicturesForRecordetail();
	 * pictures.add(picture.iterator().next()); found = 1; break; } } } } } else {
	 * if (eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getAdultertypesForEvent().contains(
	 * adultertypeDAO.findAdultertypeById(evenType))) { dates.add((Calendar)
	 * earliestRecordOfEachEvent[i][1]); Set<Eventdetail> records =
	 * eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getRecordsForEvent(); int found = 0; for
	 * (Eventdetail r : records) { if (found == 1) { break; } else if
	 * (r.getRecordetailsForRecord().size() != 0) { Set<Eventdetailreport>
	 * recordetails = r.getRecordetailsForRecord(); for (Eventdetailreport rd :
	 * recordetails) { if (rd.getPicturesForRecordetail().size() != 0) {
	 * Set<Picture> picture = rd.getPicturesForRecordetail();
	 * pictures.add(picture.iterator().next()); found = 1; break; } } } } } } } else
	 * { Calendar now = Calendar.getInstance(); Calendar c = (Calendar) now.clone();
	 * c.clear(); c.setTime(now.getTime()); c.add(Calendar.MONTH, -timeQuantum); if
	 * (evenType == 0) { if (c.compareTo((Calendar) earliestRecordOfEachEvent[i][1])
	 * < 0) { dates.add((Calendar) earliestRecordOfEachEvent[i][1]);
	 * Set<Eventdetail> records = eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getRecordsForEvent(); int found = 0; for
	 * (Eventdetail r : records) { if (found == 1) { break; } else if
	 * (r.getRecordetailsForRecord().size() != 0) { Set<Eventdetailreport>
	 * recordetails = r.getRecordetailsForRecord(); for (Eventdetailreport rd :
	 * recordetails) { if (rd.getPicturesForRecordetail().size() != 0) {
	 * Set<Picture> picture = rd.getPicturesForRecordetail();
	 * pictures.add(picture.iterator().next()); found = 1; break; } } } } } } else {
	 * if (eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getAdultertypesForEvent().contains(
	 * adultertypeDAO.findAdultertypeById(evenType)) && c.compareTo((Calendar)
	 * earliestRecordOfEachEvent[i][1]) < 0) { dates.add((Calendar)
	 * earliestRecordOfEachEvent[i][1]); Set<Eventdetail> records =
	 * eventService.findEventByPrimaryKey((int)
	 * earliestRecordOfEachEvent[i][0]).getRecordsForEvent(); int found = 0; for
	 * (Eventdetail r : records) { if (found == 1) { break; } else if
	 * (r.getRecordetailsForRecord().size() != 0) { Set<Eventdetailreport>
	 * recordetails = r.getRecordetailsForRecord(); for (Eventdetailreport rd :
	 * recordetails) { if (rd.getPicturesForRecordetail().size() != 0) {
	 * Set<Picture> picture = rd.getPicturesForRecordetail();
	 * pictures.add(picture.iterator().next()); found = 1; break; } } } } } } } }
	 * 
	 * Set<String> descriptions = new LinkedHashSet<String>(); for (Picture picture
	 * : pictures) { String[] sens =
	 * picture.getRecordetail().getRecord().getEvent().getDescription().split("。");
	 * sens[0] += "..."; descriptions.add(sens[0]); } return descriptions; }
	 */

}
